home *** CD-ROM | disk | FTP | other *** search
- /* ///////////////////////////////////////////////////////////////////
- // $Id: arcdraw.c,v 1.0 1995/08/29 06:03:13 Exp $
- // $Log: arcdraw.c.c,v $
- *
- * Revision 1.0 1.0 1995/08/29 06:03:13
- * Acorn RISC OS draw driver, contributed by Maurizio Ferrari
- * (ferrari@bologna.marelli.it).
- *
- //
- // File: arcdraw.c
- //
- // Descript: RISC OS draw file drivers
- //
- // Library: ---
- //
- // Requires: ---
- //
- // Public: plD_init_arcdraw()
- // plD_line_arcdraw()
- // plD_polyline_arcdraw()
- // plD_eop_arcdraw()
- // plD_bop_arcdraw()
- // plD_tidy_arcdraw()
- // plD_state_arcdraw()
- // plD_esc_arcdraw()
- //
- // pldummy_arcdraw()
- //
- // Private: arcdraw_initialize_pls()
- //
- // Notes: ---
- //
- // Revisions:
- /////////////////////////////////////////////////////////////////// */
-
- #include "plDevs.h"
-
- #if defined(PLD_arcdraw)
-
- /*#define DEBUG_ENTER*/
-
- #include "plplotP.h"
- #include <stdio.h>
- #include <string.h>
- #include "drivers.h"
- #include "arcdraw.h"
-
- /* top level declarations */
- /* Page sizes */
-
- #define ARCDRAW_XMIN 100
- #define ARCDRAW_XMAX 5000
- #define ARCDRAW_YMIN 200
- #define ARCDRAW_YMAX 3500
- #define ARCDRAW_XRATIO 4.
- #define ARCDRAW_YRATIO 4.
- #define SCALE_FACTOR 100
- #define TRANSPARENT 0xFFFFFFFF
- #define BLACK 0x00000000
- #define PATH_BUFFER_SIZE 32000
- #define FREE(ptr) if((ptr)!=NULL){free((void *)ptr);ptr=NULL;}
-
- enum {AT_BOP, DRAWING, AT_EOP};
-
- /* Struct to hold device-specific info. */
-
- typedef struct {
- PLFLT pxlx, pxly;
- PLINT xold, yold;
-
- PLINT xmin, xmax, xlen;
- PLINT ymin, ymax, ylen;
-
- PLINT xmin_dev, xmax_dev, xlen_dev;
- PLINT ymin_dev, ymax_dev, ylen_dev;
-
- PLFLT xscale_dev, yscale_dev;
-
- PLINT path_is_open;
- PLINT width_has_changed;
- PLINT color_0_has_changed;
- PLINT color_1_has_changed;
- PLINT x1st;
- PLINT y1st;
- PLINT xlast;
- PLINT ylast;
- PLINT fill_req;
- PLINT must_close;
- PLINT path_is_valid;
- PLColor fill_color;
- PLINT graph_isopen;
- PLINT path_offset;
- PLINT *path_ptr;
- PLINT path_len;
- } DrawDev;
-
-
- /*local prototypes */
- static void arcdraw_end_path(PLStream *, pldraw_pathstrhdr * );
- static void arcdraw_start_path(PLStream *);
- static void arcdraw_initialize_pls(PLStream *);
- static void arcdraw_change_colour0(PLStream *);
- static void arcdraw_change_colour1(PLStream *);
- static void arcdraw_change_width(PLStream *);
-
- static int make_file_hdr(PLStream *pls, pldraw_fileheader *, pldraw_bboxtyp *);
- static int make_path_hdr(PLStream *, pldraw_pathstrhdr *, pldraw_pathstyle *, pldraw_bboxtyp *);
- static int end_obj(PLStream *pls, pldraw_pathstrhdr *);
- static int add_to_path(PLStream *pls, int);
- static int move_to(PLStream *pls, int, int);
- static int line_to(PLStream *pls, int, int);
- static int close_path(PLStream *pls);
- static int end_path(PLStream *pls);
- static int rectangle(PLStream *pls, int , int , int , int );
- static void set_bbox(PLStream *pls, int x0, int y0, int x1, int y1, pldraw_bboxtyp * bbp);
- static unsigned int set_colour(PLColor);
-
- pldraw_fileheader filehdr, *fhp = &filehdr;
- pldraw_pathstrhdr objh, *oph = &objh;
- pldraw_pathstyle pstyle, *ps = &pstyle;
- pldraw_bboxtyp bbox, rect_box, *pbb = &bbox, *rbp = &rect_box;
-
- /*----------------------------------------------------------------------*\
- * arcdraw_initialize_pls()
- *
- * Initialize plot stream
- \*----------------------------------------------------------------------*/
-
- static void
- arcdraw_initialize_pls(PLStream *pls)
- {
-
- DrawDev *dev;
-
- dbug_enter("arcdraw_initialize_pls");
- pls->termin = 0; /* not an interactive terminal */
- pls->icol1 = 1;
- pls->bytecnt = 0;
- pls->page = 0;
- pls->family = 1; /* file has family */
- pls->output_type = 0; /* 0 for file output type */
- pls->dev_fill0 = 1; /* Can do solid fills */
- pls->dev_fill1 = 1; /* Can do pattern fills */
- pls->color = 1;
- pls->nopause = 1;
-
- plFamInit(pls); /* Initialize family file info */
-
- plOpenFile(pls); /* get file name if not already set */
-
-
- dbug_enter("arcdraw_initialize_pls: allocating DrawDev memory");
- pls->dev = calloc(1, (size_t) sizeof(DrawDev));
- if (pls->dev == NULL) {
- plexit("arcdraw_init: Out of memory.");
- }
-
- dev = (DrawDev *) pls->dev;
- /*clear local work variables*/
- dev->path_is_open = FALSE;
- dev->width_has_changed = FALSE;
- dev->color_0_has_changed = FALSE;
- dev->color_1_has_changed = FALSE;
- dev->x1st = 0;
- dev->y1st = 0;
- dev->xlast = 0;
- dev->ylast = 0;
- dev->fill_req = FALSE;
- dev->must_close = FALSE;
- dev->path_is_valid = FALSE;
- dev->graph_isopen = TRUE;
- dev->xmin = ARCDRAW_XMIN;
- dev->xmax = ARCDRAW_XMAX;
- dev->ymin = ARCDRAW_YMIN;
- dev->ymax = ARCDRAW_YMAX;
-
- dev->xold = UNDEFINED;
- dev->yold = UNDEFINED;
- dev->xlen = dev->xmax - dev->xmin;
- dev->ylen = dev->ymax - dev->ymin;
- dev->xscale_dev = SCALE_FACTOR;
- dev->yscale_dev = SCALE_FACTOR;
- dev->path_offset = 0;
- dev->path_len = PATH_BUFFER_SIZE;
- dev->path_ptr = (int*) calloc(dev->path_len, sizeof(int));
- if (dev->path_ptr == NULL) {
- plexit("plD_bop_arcdraw: failed to allocate memory for Draw path");
- }
-
-
- plP_setpxl((PLFLT) ARCDRAW_XRATIO, (PLFLT) ARCDRAW_YRATIO);
- plP_setphy(dev->xmin, dev->xmax, dev->ymin, dev->ymax);
- dbug_enter("arcdraw_initialize_pls: exiting");
- }
-
- /*----------------------------------------------------------------------*\
- * plD_init_arcdraw()
- *
- * Initialize device.
- \*----------------------------------------------------------------------*/
-
- void
- plD_init_arcdraw(PLStream *pls)
- {
- arcdraw_initialize_pls(pls); /* initialize plot stream */
- }
-
-
- /*----------------------------------------------------------------------*\
- * plD_bop_arcdraw()
- *
- * Set up for the next page.
- * Advance to next family file if necessary (file output).
- \*----------------------------------------------------------------------*/
-
- void
- plD_bop_arcdraw(PLStream *pls)
- {
- DrawDev *dev = (DrawDev *) pls->dev;
-
- dev->xold = UNDEFINED;
- dev->yold = UNDEFINED;
- dbug_enter("plD_bop_arcdraw");
- fflush(pls->OutFile);
- dbug_enter("plD_bop_arcdraw: plGetFam");
- if (!pls->termin) {
- plGetFam(pls);
- }
- pls->page++;
- pls->famadv = 1; /*advance to next member*/
- make_file_hdr(pls, fhp, pbb);
- /* uncomment all this to create a default black back rectangle
- * make_path_hdr(pls, oph, ps, pbb);
- * oph->fillcolour = BLACK;
- * rectangle(pls, 0, 0, 5380, 3805);
- * pls->bytecnt += 15;
- * end_path(pls);
- * pls->bytecnt += 1;
- * end_obj(pls, oph);
- * dev->path_is_open = FALSE;
- */
- make_path_hdr(pls, oph, ps, pbb);
- oph->fillcolour = set_colour(pls->cmap0[0]);
- rectangle(pls, 0, 0, 5380, 3805);
- pls->bytecnt += 15;
- end_path(pls);
- pls->bytecnt += 1;
- end_obj(pls, oph);
- dev->path_is_open = FALSE;
-
- dbug_enter("plD_bop_arcdraw: exiting");
- }
-
-
- /*----------------------------------------------------------------------*\
- * plD_eop_arcdraw()
- *
- * End of page.
- \*----------------------------------------------------------------------*/
-
- void
- plD_eop_arcdraw(PLStream *pls)
- {
- DrawDev *dev = (DrawDev *) pls->dev;
-
- dbug_enter("plD_eop_arcdraw");
- if (dev->path_is_open != FALSE) {
- arcdraw_end_path(pls, oph);
- dev->path_is_open = FALSE;
- }
- }
-
- /*----------------------------------------------------------------------*\
- * plD_tidy_arcdraw()
- *
- * Close graphics file or otherwise clean up.
- \*----------------------------------------------------------------------*/
-
- void
- plD_tidy_arcdraw(PLStream *pls)
- {
- DrawDev *dev = (DrawDev *) pls->dev;
- #ifdef __riscos
- char *sys_string;
- #endif
- dbug_enter("plD_tidy_arcdraw");
- if (dev->path_is_open != FALSE) {
- arcdraw_end_path(pls, oph);
- dev->path_is_open = FALSE;
- }
- fclose(pls->OutFile);
- if (dev->path_ptr != NULL) {
- dbug_enter("plD_tidy_arcdraw: freeing dev->path_ptr");
- FREE(dev->path_ptr);
- }
-
- if (pls->dev != NULL) {
- dbug_enter("plD_tidy_arcdraw: freeing pls->dev");
- FREE(pls->dev);
- }
- #ifdef __riscos
- sys_string=malloc(sizeof(pls->FileName)+20);
- if (sys_string == NULL) {
- plwarn("malloc failed - cannot change filetype to AFF");
- }
- else {
- sprintf(sys_string, "settype %s AFF", pls->FileName);
- system(sys_string);
- sprintf(sys_string, "filer_run %s", pls->FileName);
- system(sys_string);
- }
- free(sys_string);
- #endif
- dev->graph_isopen = FALSE;
- }
-
- /*----------------------------------------------------------------------*\
- * plD_state_arcdraw()
- *
- * Handle change in PLStream state (color, pen width, fill attribute, etc).
- \*----------------------------------------------------------------------*/
-
- void
- plD_state_arcdraw(PLStream *pls, PLINT op)
- {
- DrawDev *dev = (DrawDev *) pls->dev;
-
- switch (op) {
-
- case PLSTATE_WIDTH:
- dev->width_has_changed = TRUE;
- break;
-
- case PLSTATE_COLOR0:
- dev->color_0_has_changed = TRUE;
- break;
-
- case PLSTATE_COLOR1:
- dev->color_1_has_changed = TRUE;
- break;
-
- case PLSTATE_FILL:
- break;
-
- default:
- fprintf (stderr, "#PLSTATE_DEFAULT - code = %d ignored\n", op );
- break;
- }
- }
-
- /*----------------------------------------------------------------------*\
- * plD_esc_arcdraw()
- *
- * Escape function.
- \*----------------------------------------------------------------------*/
-
- void
- plD_esc_arcdraw(PLStream *pls, PLINT op, void *ptr)
- {
- DrawDev *dev = (DrawDev *) pls->dev;
-
- switch (op) {
-
- case PLESC_FILL:
- dev->fill_req = TRUE;
- dev->fill_color = pls->curcolor;
- plD_polyline_arcdraw(pls, pls->dev_x, pls->dev_y, pls->dev_npts);
- break;
-
- default:
- /* fprintf(stderr, "#PLESC_DEFAULT - code = %d ignored\n", op );*/
- break;
- }
-
- }
-
- /*----------------------------------------------------------------------*\
- * plD_line_arcdraw()
- *
- * Draw a line in the current color from (x1,y1) to (x2,y2).
- \*----------------------------------------------------------------------*/
-
- void
- plD_line_arcdraw(PLStream *pls, short x1a, short y1a, short x2a, short y2a)
- {
- DrawDev *dev = (DrawDev *) pls->dev;
- int x1 = x1a, y1 = y1a, x2 = x2a, y2 = y2a;
-
- /* dbug_enter("plD_line_arcdraw");*/
- /* Write out old path */
- if (x1 != dev->xold || y1 != dev->yold) {
- if (dev->path_is_open != FALSE) {
- arcdraw_end_path(pls, oph);
- }
- arcdraw_start_path(pls);
- pls->bytecnt += 3;
- move_to (pls, x1, y1);
- pls->bytecnt += 3;
- dev->x1st = x1; dev->y1st = y1;
- dev->color_0_has_changed = FALSE;
- dev->color_1_has_changed = FALSE;
- }
- /* Add new point to path */
- if (dev->width_has_changed == TRUE){
- arcdraw_change_width(pls);
- }
- if (dev->color_0_has_changed == TRUE) {
- arcdraw_change_colour0(pls);
- }
- if (dev->color_1_has_changed == TRUE) {
- arcdraw_change_colour1(pls);
- }
- line_to (pls, x2, y2);
- pls->bytecnt += 3;
- dev->xlast = x2; dev->ylast = y2;
- if ((dev->x1st != dev->xlast) || (dev->y1st != dev->ylast)) {
- dev->path_is_valid = TRUE;
- }
-
- dev->xold = x2;
- dev->yold = y2;
- }
-
- /*----------------------------------------------------------------------*\
- * plD_polyline_arcdraw()
- *
- * Draw a polyline in the current color.
- \*----------------------------------------------------------------------*/
-
- void
- plD_polyline_arcdraw(PLStream *pls, short *xa, short *ya, PLINT npts)
- {
- register PLINT i;
- DrawDev *dev = (DrawDev *) pls->dev;
-
- /* Write out old path */
- if (dev->path_is_open != FALSE) {
- arcdraw_end_path(pls, oph);
- }
- arcdraw_start_path(pls);
- move_to (pls, xa[0], ya[0]);
- pls->bytecnt += 3;
- dev->x1st = xa[0]; dev->y1st = ya[0];
- for (i = 1; i < npts; i++) { /* Add new point to path */
- line_to (pls, xa[i], ya[i]);
- pls->bytecnt += 3;
- dev->xlast = xa[i]; dev->ylast = ya[i] ;
- if ((dev->x1st != dev->xlast) || (dev->y1st != dev->ylast)) {
- dev->path_is_valid = TRUE;
- }
- }
- arcdraw_end_path(pls, oph);
- dev->xold = xa[ npts - 1 ];
- dev->yold = ya[ npts - 1 ];
- }
-
-
- /*----------------------------------------------------------------------*\
- * arcdraw_start_path()
- *
- * start a new path function.
- \*----------------------------------------------------------------------*/
-
- static void
- arcdraw_start_path(PLStream *pls)
- {
- DrawDev *dev = (DrawDev *) pls->dev;
-
- dev->path_is_open = TRUE;
- dev->path_is_valid = FALSE;
- if ( pls->width == 0 ) {/*i.e. not initialised*/
- /* draw_in_ptr->pathwidth = 500;*/
- } else {
- /* draw_in_ptr->pathwidth = 500* pls->width;*/
- }
- make_path_hdr(pls, oph, ps, pbb);
- if (dev->fill_req == TRUE) {
- dev->fill_req = FALSE;
- dev->must_close = TRUE;
- }
- dev->color_0_has_changed = FALSE;
- dev->color_1_has_changed = FALSE;
-
- }
-
- /*----------------------------------------------------------------------*\
- * arcdraw_start_path()
- *
- * start a new path function.
- \*----------------------------------------------------------------------*/
-
- static void
- arcdraw_end_path(PLStream *pls, pldraw_pathstrhdr * oph)
- {
- DrawDev *dev = (DrawDev *) pls->dev;
-
- /* this should patch rogue draw paths */
- if (dev->path_is_valid == FALSE) {
- line_to (pls, dev->xlast+1, dev->ylast+1);
- pls->bytecnt += 3;
- }
- if (dev->must_close == TRUE) {
- oph->fillcolour = set_colour(pls->curcolor);
- close_path(pls);
- pls->bytecnt += 1;
- dev->must_close = FALSE;
- }
- end_path(pls);
- pls->bytecnt += 1;
- end_obj(pls, oph);
- dev->path_is_open = FALSE;
- }
-
-
- /*----------------------------------------------------------------------*\
- * arcdraw_change_colour0()
- *
- * change colour0 function.
- \*----------------------------------------------------------------------*/
-
- static void
- arcdraw_change_colour0(PLStream *pls)
- {
- DrawDev *dev = (DrawDev *) pls->dev;
-
- dev->color_0_has_changed = FALSE;
-
- }
-
- /*----------------------------------------------------------------------*\
- * arcdraw_change_colour1()
- *
- * change colour1 function.
- \*----------------------------------------------------------------------*/
-
- static void
- arcdraw_change_colour1(PLStream *pls)
- {
- DrawDev *dev = (DrawDev *) pls->dev;
-
- dev->color_1_has_changed = FALSE;
- }
-
- /*----------------------------------------------------------------------*\
- * arcdraw_change_width()
- *
- * change colour1 function.
- \*----------------------------------------------------------------------*/
-
- static void
- arcdraw_change_width(PLStream *pls)
- {
- DrawDev *dev = (DrawDev *) pls->dev;
-
- dev->width_has_changed = FALSE;
- }
-
- /*----------------------------------------------------------------------*\
- * set_bbox()
- *
- * set draw file bounding box.
- \*----------------------------------------------------------------------*/
-
- void
- set_bbox(PLStream *pls, int x0, int y0, int x1, int y1, pldraw_bboxtyp * bbp)
- {
- bbp->x0 = x0;
- bbp->y0 = y0;
- bbp->x1 = x1;
- bbp->y1 = y1;
- }
-
- /*----------------------------------------------------------------------*\
- * make_file_hdr()
- *
- * create draw file header.
- \*----------------------------------------------------------------------*/
- int
- make_file_hdr(PLStream *pls, pldraw_fileheader * fhp, pldraw_bboxtyp * pbb)
- {
- int i = 0;
- char *type = "Draw", *appl = "PLPlot ";
-
- while ((fhp->title[i++] = *type++) != '\0');
- i = 0;
- while ((fhp->progident[i++] = *appl++) != '\0');
- fhp->majorstamp = 201;
- fhp->minorstamp = 0;
- set_bbox(pls, 0, 0, 0, 0, pbb);
- fhp->bbox = *pbb;
- fwrite(fhp, 1, sizeof(*fhp), pls->OutFile);
- return (0);
- }
-
- /*----------------------------------------------------------------------*\
- * set_colour()
- *
- * set draw file line colour.
- \*----------------------------------------------------------------------*/
- static unsigned int
- set_colour(PLColor color)
- {
- return (color.r<<8)|(color.g<<16)|(color.b<<24);
- }
-
-
- /*----------------------------------------------------------------------*\
- * make_path_hdr()
- *
- * create draw file path header.
- \*----------------------------------------------------------------------*/
- int
- make_path_hdr(PLStream *pls, pldraw_pathstrhdr * oph, pldraw_pathstyle * ps, pldraw_bboxtyp * pbb)
- {
- DrawDev *dev = (DrawDev *) pls->dev;
-
- oph->tag = pldraw_OBJPATH;
- set_bbox(pls, 0, 0, 100000, 506000, pbb);
- oph->bbox = *pbb;
- oph->fillcolour = TRANSPARENT;
- oph->pathcolour = set_colour(pls->curcolor);
- oph->pathwidth = (int)(pls->width*dev->xscale_dev);
- ps->joincapwind = 0x42;
- ps->reserved8 = 0; /* do NOT alter this */
- ps->tricapwid = 10;
- ps->tricaphei = 32;
- oph->pathstyle = *ps;
- return (0);
- }
-
- /*----------------------------------------------------------------------*\
- * end_obj()
- *
- * end draw file object.
- \*----------------------------------------------------------------------*/
- int
- end_obj(PLStream *pls, pldraw_pathstrhdr * oph)
- {
- DrawDev *dev = (DrawDev *) pls->dev;
-
- oph->size = 4 * (10 + dev->path_offset);
- fwrite(oph, 1, sizeof(*oph), pls->OutFile);
- fwrite(dev->path_ptr, 1, 4 * dev->path_offset, pls->OutFile);
- dev->path_offset = 0;
- return (0);
- }
-
- /*----------------------------------------------------------------------*\
- * add_to_path()
- *
- * add point to draw file path.
- \*----------------------------------------------------------------------*/
- int
- add_to_path(PLStream *pls, int value)
- {
- DrawDev *dev = (DrawDev *) pls->dev;
-
- if (dev->path_offset + 1 > dev->path_len)
- return (-1);
- else {
- dev->path_ptr[0 + dev->path_offset] = value;
- return (dev->path_offset = dev->path_offset + 1);
- };
- }
-
-
- /*----------------------------------------------------------------------*\
- * move_to()
- *
- * move to selected location.
- \*----------------------------------------------------------------------*/
- int
- move_to(PLStream *pls, int x, int y)
- {
- add_to_path(pls, 2);
- add_to_path(pls, x*SCALE_FACTOR);
- add_to_path(pls, y*SCALE_FACTOR);
- return (0);
- }
-
- /*----------------------------------------------------------------------*\
- * line_to()
- *
- * draw a line to selected location.
- \*----------------------------------------------------------------------*/
- int
- line_to(PLStream *pls, int x, int y)
- {
- DrawDev *dev = (DrawDev *) pls->dev;
-
- add_to_path(pls, 8);
- add_to_path(pls, (int)(x*dev->xscale_dev));
- add_to_path(pls, (int)(y*dev->yscale_dev));
- return (0);
- }
-
-
- /*----------------------------------------------------------------------*\
- * close_path()
- *
- * close draw file path.
- \*----------------------------------------------------------------------*/
- int
- close_path(PLStream *pls)
- {
- add_to_path(pls, 5);
- return (0);
- }
-
-
- /*----------------------------------------------------------------------*\
- * end_path()
- *
- * end draw file path.
- \*----------------------------------------------------------------------*/
- int
- end_path(PLStream *pls)
- {
- add_to_path(pls, 0);
- return (0);
- }
-
-
- /*----------------------------------------------------------------------*\
- * rectangle()
- *
- * draw a rectangle of given coordinates.
- \*----------------------------------------------------------------------*/
- int
- rectangle(PLStream *pls, int x0, int y0, int x1, int y1)
- {
- move_to(pls, x0, y0);
- line_to(pls, x0, y1);
- line_to(pls, x1, y1);
- line_to(pls, x1, y0);
- line_to(pls, x0, y0);
- return (0);
- }
-
-
- #else
- int
- pldummy_arcdraw(void)
- {
- return 0;
- }
-
- #endif /* PLD_arcdraw */
-
-